<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Chapter 5 Sequence Input/Output &mdash; Biopython_en 1.0 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Biopython_en 1.0 documentation" href="index.html" />
    <link rel="next" title="Chapter 6 Multiple Sequence Alignment objects" href="chr6.html" />
    <link rel="prev" title="Chapter 4 Sequence annotation objects" href="chr4.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="chr6.html" title="Chapter 6 Multiple Sequence Alignment objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="chr4.html" title="Chapter 4 Sequence annotation objects"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Biopython_en 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="chapter-5-sequence-input-output">
<h1>Chapter 5  Sequence Input/Output<a class="headerlink" href="#chapter-5-sequence-input-output" title="Permalink to this headline">¶</a></h1>
<p>In this chapter we’ll discuss in more detail the <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> module,
which was briefly introduced in Chapter <a class="reference external" href="#chapter:quick-start">2</a>
and also used in Chapter <a class="reference external" href="#chapter:SeqRecord">4</a>. This aims to
provide a simple interface for working with assorted sequence file
formats in a uniform way. See also the <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> wiki page
(<tt class="docutils literal"><span class="pre">`http://biopython.org/wiki/SeqIO</span></tt> &lt;<a class="reference external" href="http://biopython.org/wiki/SeqIO">http://biopython.org/wiki/SeqIO</a>&gt;`__),
and the built in documentation (also
<a class="reference external" href="http://biopython.org/DIST/docs/api/Bio.SeqIO-module.html">online</a>):</p>
<p>The “catch” is that you have to work with <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects (see
Chapter <a class="reference external" href="#chapter:SeqRecord">4</a>), which contain a <tt class="docutils literal"><span class="pre">Seq</span></tt> object
(see Chapter <a class="reference external" href="#chapter:Bio.Seq">3</a>) plus annotation like an
identifier and description.</p>
<div class="section" id="parsing-or-reading-sequences">
<h2>5.1  Parsing or Reading Sequences<a class="headerlink" href="#parsing-or-reading-sequences" title="Permalink to this headline">¶</a></h2>
<p>The workhorse function <tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> is used to read in sequence
data as SeqRecord objects. This function expects two arguments:</p>
<ol class="arabic simple">
<li>The first argument is a <em>handle</em> to read the data from, or a
filename. A handle is typically a file opened for reading, but could
be the output from a command line program, or data downloaded from
the internet (see Section <a class="reference external" href="#sec:SeqIO_Online">5.3</a>). See
Section <a class="reference external" href="#sec:appendix-handles">22.1</a> for more about handles.</li>
<li>The second argument is a lower case string specifying sequence format
– we don’t try and guess the file format for you! See
<tt class="docutils literal"><span class="pre">`http://biopython.org/wiki/SeqIO</span></tt> &lt;<a class="reference external" href="http://biopython.org/wiki/SeqIO">http://biopython.org/wiki/SeqIO</a>&gt;`__
for a full listing of supported formats.</li>
</ol>
<p>There is an optional argument <tt class="docutils literal"><span class="pre">alphabet</span></tt> to specify the alphabet to be
used. This is useful for file formats like FASTA where otherwise
<tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> will default to a generic alphabet.</p>
<p>The <tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> function returns an <em>iterator</em> which gives
<tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects. Iterators are typically used in a for loop as
shown below.</p>
<p>Sometimes you’ll find yourself dealing with files which contain only a
single record. For this situation use the function <tt class="docutils literal"><span class="pre">Bio.SeqIO.read()</span></tt>
which takes the same arguments. Provided there is one and only one
record in the file, this is returned as a <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object.
Otherwise an exception is raised.</p>
<div class="section" id="reading-sequence-files">
<h3>5.1.1  Reading Sequence Files<a class="headerlink" href="#reading-sequence-files" title="Permalink to this headline">¶</a></h3>
<p>In general <tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> is used to read in sequence files as
<tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects, and is typically used with a for loop like this:</p>
<p>The above example is repeated from the introduction in
Section <a class="reference external" href="#sec:sequence-parsing">2.4</a>, and will load the orchid DNA
sequences in the FASTA format file
<a class="reference external" href="http://biopython.org/DIST/docs/tutorial/examples/ls_orchid.fasta">ls_orchid.fasta</a>.
If instead you wanted to load a GenBank format file like
<a class="reference external" href="http://biopython.org/DIST/docs/tutorial/examples/ls_orchid.gbk">ls_orchid.gbk</a>
then all you need to do is change the filename and the format string:</p>
<p>Similarly, if you wanted to read in a file in another file format, then
assuming <tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> supports it you would just need to change
the format string as appropriate, for example “swiss” for SwissProt
files or “embl” for EMBL text files. There is a full listing on the wiki
page
(<tt class="docutils literal"><span class="pre">`http://biopython.org/wiki/SeqIO</span></tt> &lt;<a class="reference external" href="http://biopython.org/wiki/SeqIO">http://biopython.org/wiki/SeqIO</a>&gt;`__)
and in the built in documentation (also
<a class="reference external" href="http://biopython.org/DIST/docs/api/Bio.SeqIO-module.html">online</a>).</p>
<p>Another very common way to use a Python iterator is within a list
comprehension (or a generator expression). For example, if all you
wanted to extract from the file was a list of the record identifiers we
can easily do this with the following list comprehension:</p>
<p>There are more examples using <tt class="docutils literal"><span class="pre">SeqIO.parse()</span></tt> in a list comprehension
like this in Section <a class="reference external" href="#seq:sequence-parsing-plus-pylab">18.2</a> (e.g.
for plotting sequence lengths or GC%).</p>
</div>
<div class="section" id="iterating-over-the-records-in-a-sequence-file">
<h3>5.1.2  Iterating over the records in a sequence file<a class="headerlink" href="#iterating-over-the-records-in-a-sequence-file" title="Permalink to this headline">¶</a></h3>
<p>In the above examples, we have usually used a for loop to iterate over
all the records one by one. You can use the for loop with all sorts of
Python objects (including lists, tuples and strings) which support the
iteration interface.</p>
<p>The object returned by <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> is actually an iterator which
returns <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects. You get to see each record in turn, but
once and only once. The plus point is that an iterator can save you
memory when dealing with large files.</p>
<p>Instead of using a for loop, can also use the <tt class="docutils literal"><span class="pre">.next()</span></tt> method of an
iterator to step through the entries, like this:</p>
<p>Note that if you try and use <tt class="docutils literal"><span class="pre">.next()</span></tt> and there are no more results,
you’ll get the special <tt class="docutils literal"><span class="pre">StopIteration</span></tt> exception.</p>
<p>One special case to consider is when your sequence files have multiple
records, but you only want the first one. In this situation the
following code is very concise:</p>
<p>A word of warning here – using the <tt class="docutils literal"><span class="pre">.next()</span></tt> method like this will
silently ignore any additional records in the file. If your files have
<em>one and only one</em> record, like some of the online examples later in
this chapter, or a GenBank file for a single chromosome, then use the
new <tt class="docutils literal"><span class="pre">Bio.SeqIO.read()</span></tt> function instead. This will check there are no
extra unexpected records present.</p>
</div>
<div class="section" id="getting-a-list-of-the-records-in-a-sequence-file">
<h3>5.1.3  Getting a list of the records in a sequence file<a class="headerlink" href="#getting-a-list-of-the-records-in-a-sequence-file" title="Permalink to this headline">¶</a></h3>
<p>In the previous section we talked about the fact that
<tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> gives you a <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> iterator, and that you
get the records one by one. Very often you need to be able to access the
records in any order. The Python <tt class="docutils literal"><span class="pre">list</span></tt> data type is perfect for this,
and we can turn the record iterator into a list of <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects
using the built-in Python function <tt class="docutils literal"><span class="pre">list()</span></tt> like so:</p>
<p>Giving:</p>
<p>You can of course still use a for loop with a list of <tt class="docutils literal"><span class="pre">SeqRecord</span></tt>
objects. Using a list is much more flexible than an iterator (for
example, you can determine the number of records from the length of the
list), but does need more memory because it will hold all the records in
memory at once.</p>
</div>
<div class="section" id="extracting-data">
<h3>5.1.4  Extracting data<a class="headerlink" href="#extracting-data" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object and its annotation structures are described
more fully in Chapter <a class="reference external" href="#chapter:SeqRecord">4</a>. As an example of how
annotations are stored, we’ll look at the output from parsing the first
record in the GenBank file
<a class="reference external" href="http://biopython.org/DIST/docs/tutorial/examples/ls_orchid.gbk">ls_orchid.gbk</a>.</p>
<p>That should give something like this:</p>
<p>This gives a human readable summary of most of the annotation data for
the <tt class="docutils literal"><span class="pre">SeqRecord</span></tt>. For this example we’re going to use the
<tt class="docutils literal"><span class="pre">.annotations</span></tt> attribute which is just a Python dictionary. The
contents of this annotations dictionary were shown when we printed the
record above. You can also print them out directly:</p>
<p>Like any Python dictionary, you can easily get a list of the keys:</p>
<p>or values:</p>
<p>In general, the annotation values are strings, or lists of strings. One
special case is any references in the file get stored as reference
objects.</p>
<p>Suppose you wanted to extract a list of the species from the
<a class="reference external" href="http://biopython.org/DIST/docs/tutorial/examples/ls_orchid.gbk">ls_orchid.gbk</a>
GenBank file. The information we want, <em>Cypripedium irapeanum</em>, is held
in the annotations dictionary under ‘source’ and ‘organism’, which we
can access like this:</p>
<p>or:</p>
<p>In general, ‘organism’ is used for the scientific name (in Latin, e.g.
<em>Arabidopsis thaliana</em>), while ‘source’ will often be the common name
(e.g. thale cress). In this example, as is often the case, the two
fields are identical.</p>
<p>Now let’s go through all the records, building up a list of the species
each orchid sequence is from:</p>
<p>Another way of writing this code is to use a list comprehension:</p>
<p>In either case, the result is:</p>
<p>Great. That was pretty easy because GenBank files are annotated in a
standardised way.</p>
<p>Now, let’s suppose you wanted to extract a list of the species from a
FASTA file, rather than the GenBank file. The bad news is you will have
to write some code to extract the data you want from the record’s
description line - if the information is in the file in the first place!
Our example FASTA format file
<a class="reference external" href="http://biopython.org/DIST/docs/tutorial/examples/ls_orchid.fasta">ls_orchid.fasta</a>
starts like this:</p>
<p>You can check by hand, but for every record the species name is in the
description line as the second word. This means if we break up each
record’s <tt class="docutils literal"><span class="pre">.description</span></tt> at the spaces, then the species is there as
field number one (field zero is the record identifier). That means we
can do this:</p>
<p>This gives:</p>
<p>The concise alternative using list comprehensions would be:</p>
<p>In general, extracting information from the FASTA description line is
not very nice. If you can get your sequences in a well annotated file
format like GenBank or EMBL, then this sort of annotation information is
much easier to deal with.</p>
</div>
</div>
<div class="section" id="parsing-sequences-from-compressed-files">
<h2>5.2  Parsing sequences from compressed files<a class="headerlink" href="#parsing-sequences-from-compressed-files" title="Permalink to this headline">¶</a></h2>
<p>In the previous section, we looked at parsing sequence data from a file.
Instead of using a filename, you can give <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> a handle (see
Section <a class="reference external" href="#sec:appendix-handles">22.1</a>), and in this section we’ll
use handles to parse sequence from compressed files.</p>
<p>As you’ll have seen above, we can use <tt class="docutils literal"><span class="pre">Bio.SeqIO.read()</span></tt> or
<tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> with a filename - for instance this quick example
calculates the total length of the sequences in a multiple record
GenBank file using a generator expression:</p>
<p>Here we use a file handle instead, using the <tt class="docutils literal"><span class="pre">with</span></tt> statement (Python
2.5 or later) to close the handle automatically:</p>
<p>Or, the old fashioned way where you manually close the handle:</p>
<p>Now, suppose we have a gzip compressed file instead? These are very
commonly used on Linux. We can use Python’s <tt class="docutils literal"><span class="pre">gzip</span></tt> module to open the
compressed file for reading - which gives us a handle object:</p>
<p>Similarly if we had a bzip2 compressed file (sadly the function name
isn’t quite as consistent):</p>
<p>If you are using Python 2.7 or later, the <tt class="docutils literal"><span class="pre">with</span></tt>-version works for
gzip and bz2 as well. Unfortunately this is broken on older versions of
Python (<a class="reference external" href="http://bugs.python.org/issue3860">Issue 3860</a>) and you’d get
an <tt class="docutils literal"><span class="pre">AttributeError</span></tt> about <tt class="docutils literal"><span class="pre">__exit__</span></tt> being missing.</p>
<p>There is a gzip (GNU Zip) variant called BGZF (Blocked GNU Zip Format),
which can be treated like an ordinary gzip file for reading, but has
advantages for random access later which we’ll talk about later in
Section <a class="reference external" href="#sec:SeqIO-index-bgzf">5.4.4</a>.</p>
</div>
<div class="section" id="parsing-sequences-from-the-net">
<h2>5.3  Parsing sequences from the net<a class="headerlink" href="#parsing-sequences-from-the-net" title="Permalink to this headline">¶</a></h2>
<p>In the previous sections, we looked at parsing sequence data from a file
(using a filename or handle), and from compressed files (using a
handle). Here we’ll use <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> with another type of handle, a
network connection, to download and parse sequences from the internet.</p>
<p>Note that just because you <em>can</em> download sequence data and parse it
into a <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object in one go doesn’t mean this is a good idea.
In general, you should probably download sequences <em>once</em> and save them
to a file for reuse.</p>
<div class="section" id="parsing-genbank-records-from-the-net">
<h3>5.3.1  Parsing GenBank records from the net<a class="headerlink" href="#parsing-genbank-records-from-the-net" title="Permalink to this headline">¶</a></h3>
<p>Section <a class="reference external" href="#sec:efetch">9.6</a> talks about the Entrez EFetch interface
in more detail, but for now let’s just connect to the NCBI and get a few
<em>Opuntia</em> (prickly-pear) sequences from GenBank using their GI numbers.</p>
<p>First of all, let’s fetch just one record. If you don’t care about the
annotations and features downloading a FASTA file is a good choice as
these are compact. Now remember, when you expect the handle to contain
one and only one record, use the <tt class="docutils literal"><span class="pre">Bio.SeqIO.read()</span></tt> function:</p>
<p>Expected output:</p>
<p>The NCBI will also let you ask for the file in other formats, in
particular as a GenBank file. Until Easter 2009, the Entrez EFetch API
let you use “genbank” as the return type, however the NCBI now insist on
using the official return types of “gb” (or “gp” for proteins) as
described on <a class="reference external" href="http://www.ncbi.nlm.nih.gov/entrez/query/static/efetchseq_help.html">EFetch for Sequence and other Molecular Biology
Databases</a>.
As a result, in Biopython 1.50 onwards, we support “gb” as an alias for
“genbank” in <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt>.</p>
<p>The expected output of this example is:</p>
<p>Notice this time we have three features.</p>
<p>Now let’s fetch several records. This time the handle contains multiple
records, so we must use the <tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> function:</p>
<p>That should give the following output:</p>
<p>See Chapter <a class="reference external" href="#chapter:entrez">9</a> for more about the <tt class="docutils literal"><span class="pre">Bio.Entrez</span></tt>
module, and make sure to read about the NCBI guidelines for using Entrez
(Section <a class="reference external" href="#sec:entrez-guidelines">9.1</a>).</p>
</div>
<div class="section" id="parsing-swissprot-sequences-from-the-net">
<h3>5.3.2  Parsing SwissProt sequences from the net<a class="headerlink" href="#parsing-swissprot-sequences-from-the-net" title="Permalink to this headline">¶</a></h3>
<p>Now let’s use a handle to download a SwissProt file from ExPASy,
something covered in more depth in
Chapter <a class="reference external" href="#chapter:swiss_prot">10</a>. As mentioned above, when you
expect the handle to contain one and only one record, use the
<tt class="docutils literal"><span class="pre">Bio.SeqIO.read()</span></tt> function:</p>
<p>Assuming your network connection is OK, you should get back:</p>
</div>
</div>
<div class="section" id="sequence-files-as-dictionaries">
<h2>5.4  Sequence files as Dictionaries<a class="headerlink" href="#sequence-files-as-dictionaries" title="Permalink to this headline">¶</a></h2>
<p>We’re now going to introduce three related functions in the
<tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> module which allow dictionary like random access to a
multi-sequence file. There is a trade off here between flexibility and
memory usage. In summary:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt> is the most flexible but also the most memory
demanding option (see Section <a class="reference external" href="#SeqIO:to_dict">5.4.1</a>). This is
basically a helper function to build a normal Python <tt class="docutils literal"><span class="pre">dictionary</span></tt>
with each entry held as a <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object in memory, allowing
you to modify the records.</li>
<li><tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> is a useful middle ground, acting like a read
only dictionary and parsing sequences into <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects on
demand (see Section <a class="reference external" href="#sec:SeqIO-index">5.4.2</a>).</li>
<li><tt class="docutils literal"><span class="pre">Bio.SeqIO.index_db()</span></tt> also acts like a read only dictionary but
stores the identifiers and file offsets in a file on disk (as an
SQLite3 database), meaning it has very low memory requirements (see
Section <a class="reference external" href="#sec:SeqIO-index-db">5.4.3</a>), but will be a little bit
slower.</li>
</ul>
<p>See the discussion for an broad overview
(Section <a class="reference external" href="#sec:SeqIO-indexing-discussion">5.4.5</a>).</p>
<div class="section" id="sequence-files-as-dictionaries-in-memory">
<h3>5.4.1  Sequence files as Dictionaries – In memory<a class="headerlink" href="#sequence-files-as-dictionaries-in-memory" title="Permalink to this headline">¶</a></h3>
<p>The next thing that we’ll do with our ubiquitous orchid files is to show
how to index them and access them like a database using the Python
<tt class="docutils literal"><span class="pre">dictionary</span></tt> data type (like a hash in Perl). This is very useful for
moderately large files where you only need to access certain elements of
the file, and makes for a nice quick ’n dirty database. For dealing with
larger files where memory becomes a problem, see
Section <a class="reference external" href="#sec:SeqIO-index">5.4.2</a> below.</p>
<p>You can use the function <tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt> to make a SeqRecord
dictionary (in memory). By default this will use each record’s
identifier (i.e. the <tt class="docutils literal"><span class="pre">.id</span></tt> attribute) as the key. Let’s try this using
our GenBank file:</p>
<p>There is just one required argument for <tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt>, a list
or generator giving <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects. Here we have just used the
output from the <tt class="docutils literal"><span class="pre">SeqIO.parse</span></tt> function. As the name suggests, this
returns a Python dictionary.</p>
<p>Since this variable <tt class="docutils literal"><span class="pre">orchid_dict</span></tt> is an ordinary Python dictionary, we
can look at all of the keys we have available:</p>
<p>If you really want to, you can even look at all the records at once:</p>
<p>We can access a single <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object via the keys and manipulate
the object as normal:</p>
<p>So, it is very easy to create an in memory “database” of our GenBank
records. Next we’ll try this for the FASTA file instead.</p>
<p>Note that those of you with prior Python experience should all be able
to construct a dictionary like this “by hand”. However, typical
dictionary construction methods will not deal with the case of repeated
keys very nicely. Using the <tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt> will explicitly
check for duplicate keys, and raise an exception if any are found.</p>
<div class="section" id="specifying-the-dictionary-keys">
<h4>5.4.1.1  Specifying the dictionary keys<a class="headerlink" href="#specifying-the-dictionary-keys" title="Permalink to this headline">¶</a></h4>
<p>Using the same code as above, but for the FASTA file instead:</p>
<p>This time the keys are:</p>
<p>You should recognise these strings from when we parsed the FASTA file
earlier in Section <a class="reference external" href="#sec:fasta-parsing">2.4.1</a>. Suppose you would
rather have something else as the keys - like the accession numbers.
This brings us nicely to <tt class="docutils literal"><span class="pre">SeqIO.to_dict()</span></tt>’s optional argument
<tt class="docutils literal"><span class="pre">key_function</span></tt>, which lets you define what to use as the dictionary
key for your records.</p>
<p>First you must write your own function to return the key you want (as a
string) when given a <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object. In general, the details of
function will depend on the sort of input records you are dealing with.
But for our orchids, we can just split up the record’s identifier using
the “pipe” character (the vertical line) and return the fourth entry
(field three):</p>
<p>Then we can give this function to the <tt class="docutils literal"><span class="pre">SeqIO.to_dict()</span></tt> function to
use in building the dictionary:</p>
<p>Finally, as desired, the new dictionary keys:</p>
<p>Not too complicated, I hope!</p>
</div>
<div class="section" id="indexing-a-dictionary-using-the-seguid-checksum">
<h4>5.4.1.2  Indexing a dictionary using the SEGUID checksum<a class="headerlink" href="#indexing-a-dictionary-using-the-seguid-checksum" title="Permalink to this headline">¶</a></h4>
<p>To give another example of working with dictionaries of <tt class="docutils literal"><span class="pre">SeqRecord</span></tt>
objects, we’ll use the SEGUID checksum function. This is a relatively
recent checksum, and collisions should be very rare (i.e. two different
sequences with the same checksum), an improvement on the CRC64 checksum.</p>
<p>Once again, working with the orchids GenBank file:</p>
<p>This should give:</p>
<p>Now, recall the <tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt> function’s <tt class="docutils literal"><span class="pre">key_function</span></tt>
argument expects a function which turns a <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> into a string.
We can’t use the <tt class="docutils literal"><span class="pre">seguid()</span></tt> function directly because it expects to be
given a <tt class="docutils literal"><span class="pre">Seq</span></tt> object (or a string). However, we can use Python’s
<tt class="docutils literal"><span class="pre">lambda</span></tt> feature to create a “one off” function to give to
<tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt> instead:</p>
<p>That should have retrieved the record <tt class="docutils literal"><span class="pre">Z78532.1</span></tt>, the second entry in
the file.</p>
</div>
</div>
<div class="section" id="sequence-files-as-dictionaries-indexed-files">
<h3>5.4.2  Sequence files as Dictionaries – Indexed files<a class="headerlink" href="#sequence-files-as-dictionaries-indexed-files" title="Permalink to this headline">¶</a></h3>
<p>As the previous couple of examples tried to illustrate, using
<tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt> is very flexible. However, because it holds
everything in memory, the size of file you can work with is limited by
your computer’s RAM. In general, this will only work on small to medium
files.</p>
<p>For larger files you should consider <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt>, which works
a little differently. Although it still returns a dictionary like
object, this does <em>not</em> keep <em>everything</em> in memory. Instead, it just
records where each record is within the file – when you ask for a
particular record, it then parses it on demand.</p>
<p>As an example, let’s use the same GenBank file as before:</p>
<p>Note that <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> won’t take a handle, but only a
filename. There are good reasons for this, but it is a little technical.
The second argument is the file format (a lower case string as used in
the other <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> functions). You can use many other simple file
formats, including FASTA and FASTQ files (see the example in
Section <a class="reference external" href="#sec:fastq-indexing">18.1.11</a>). However, alignment formats
like PHYLIP or Clustal are not supported. Finally as an optional
argument you can supply an alphabet, or a key function.</p>
<p>Here is the same example using the FASTA file - all we change is the
filename and the format name:</p>
<div class="section" id="id1">
<h4>5.4.2.1  Specifying the dictionary keys<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h4>
<p>Suppose you want to use the same keys as before? Much like with the
<tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt> example in
Section <a class="reference external" href="#seq:seqio-todict-functionkey">5.4.1.1</a>, you’ll need to
write a tiny function to map from the FASTA identifier (as a string) to
the key you want:</p>
<p>Then we can give this function to the <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> function to
use in building the dictionary:</p>
<p>Easy when you know how?</p>
</div>
<div class="section" id="getting-the-raw-data-for-a-record">
<h4>5.4.2.2  Getting the raw data for a record<a class="headerlink" href="#getting-the-raw-data-for-a-record" title="Permalink to this headline">¶</a></h4>
<p>The dictionary-like object from <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> gives you each
entry as a <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object. However, it is sometimes useful to be
able to get the original raw data straight from the file. For this use
the <tt class="docutils literal"><span class="pre">get_raw()</span></tt> method which takes a single argument (the record
identifier) and returns a string (extracted from the file without
modification).</p>
<p>A motivating example is extracting a subset of a records from a large
file where either <tt class="docutils literal"><span class="pre">Bio.SeqIO.write()</span></tt> does not (yet) support the
output file format (e.g. the plain text SwissProt file format) or where
you need to preserve the text exactly (e.g. GenBank or EMBL output from
Biopython does not yet preserve every last bit of annotation).</p>
<p>Let’s suppose you have download the whole of UniProt in the plain text
SwissPort file format from their FTP site
(<tt class="docutils literal"><span class="pre">`ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/complete/uniprot_sprot.dat.gz</span></tt> &lt;<a class="reference external" href="ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/complete/uniprot_sprot.dat.gz">ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/complete/uniprot_sprot.dat.gz</a>&gt;`__)
and uncompressed it as the file <tt class="docutils literal"><span class="pre">uniprot_sprot.dat</span></tt>, and you want to
extract just a few records from it:</p>
<p>There is a longer example in Section <a class="reference external" href="#sec:SeqIO-sort">18.1.5</a>
using the <tt class="docutils literal"><span class="pre">SeqIO.index()</span></tt> function to sort a large sequence file
(without loading everything into memory at once).</p>
</div>
</div>
<div class="section" id="sequence-files-as-dictionaries-database-indexed-files">
<h3>5.4.3  Sequence files as Dictionaries – Database indexed files<a class="headerlink" href="#sequence-files-as-dictionaries-database-indexed-files" title="Permalink to this headline">¶</a></h3>
<p>Biopython 1.57 introduced an alternative, <tt class="docutils literal"><span class="pre">Bio.SeqIO.index_db()</span></tt>,
which can work on even extremely large files since it stores the record
information as a file on disk (using an SQLite3 database) rather than in
memory. Also, you can index multiple files together (providing all the
record identifiers are unique).</p>
<p>The <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> function takes three required arguments:</p>
<ul class="simple">
<li>Index filename, we suggest using something ending <tt class="docutils literal"><span class="pre">.idx</span></tt>. This
index file is actually an SQLite3 database.</li>
<li>List of sequence filenames to index (or a single filename)</li>
<li>File format (lower case string as used in the rest of the <tt class="docutils literal"><span class="pre">SeqIO</span></tt>
module).</li>
</ul>
<p>As an example, consider the GenBank flat file releases from the NCBI FTP
site,
<tt class="docutils literal"><span class="pre">`ftp://ftp.ncbi.nih.gov/genbank/</span></tt> &lt;<a class="reference external" href="ftp://ftp.ncbi.nih.gov/genbank/">ftp://ftp.ncbi.nih.gov/genbank/</a>&gt;`__,
which are gzip compressed GenBank files. As of GenBank release 182,
there are 16 files making up the viral sequences, <tt class="docutils literal"><span class="pre">gbvrl1.seq</span></tt>, …,
<tt class="docutils literal"><span class="pre">gbvrl16.seq</span></tt>, containing in total almost one million records. You can
index them like this:</p>
<p>That takes about two minutes to run on my machine. If you rerun it then
the index file (here <tt class="docutils literal"><span class="pre">gbvrl.idx</span></tt>) is reloaded in under a second. You
can use the index as a read only Python dictionary - without having to
worry about which file the sequence comes from, e.g.</p>
<div class="section" id="id2">
<h4>5.4.3.1  Getting the raw data for a record<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h4>
<p>Just as with the <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> function discussed above in
Section <a class="reference external" href="#sec:seqio-index-getraw">5.4.2.2</a>, the dictionary like
object also lets you get at the raw text of each record:</p>
</div>
</div>
<div class="section" id="indexing-compressed-files">
<h3>5.4.4  Indexing compressed files<a class="headerlink" href="#indexing-compressed-files" title="Permalink to this headline">¶</a></h3>
<p>Very often when you are indexing a sequence file it can be quite large –
so you may want to compress it on disk. Unfortunately efficient random
access is difficult with the more common file formats like gzip and
bzip2. In this setting, BGZF (Blocked GNU Zip Format) can be very
helpful. This is a variant of gzip (and can be decompressed using
standard gzip tools) popularised by the BAM file format,
<a class="reference external" href="http://samtools.sourceforge.net/">samtools</a>, and
<a class="reference external" href="http://samtools.sourceforge.net/tabix.shtml">tabix</a>.</p>
<p>To create a BGZF compressed file you can use the command line tool
<tt class="docutils literal"><span class="pre">bgzip</span></tt> which comes with samtools. In our examples we use a filename
extension <tt class="docutils literal"><span class="pre">*.bgz</span></tt>, so they can be distinguished from normal gzipped
files (named <tt class="docutils literal"><span class="pre">*.gz</span></tt>). You can also use the <tt class="docutils literal"><span class="pre">Bio.bgzf</span></tt> module to read
and write BGZF files from within Python.</p>
<p>The <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> and <tt class="docutils literal"><span class="pre">Bio.SeqIO.index_db()</span></tt> can both be used
with BGZF compressed files. For example, if you started with an
uncompressed GenBank file:</p>
<p>You could compress this (while keeping the original file) at the command
line using the following command – but don’t worry, the compressed file
is already included with the other example files:</p>
<p>You can use the compressed file in exactly the same way:</p>
<p>or:</p>
<p>The <tt class="docutils literal"><span class="pre">SeqIO</span></tt> indexing automatically detects the BGZF compression. Note
that you can’t use the same index file for the uncompressed and
compressed files.</p>
</div>
<div class="section" id="discussion">
<h3>5.4.5  Discussion<a class="headerlink" href="#discussion" title="Permalink to this headline">¶</a></h3>
<p>So, which of these methods should you use and why? It depends on what
you are trying to do (and how much data you are dealing with). However,
in general picking <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> is a good starting point. If
you are dealing with millions of records, multiple files, or repeated
analyses, then look at <tt class="docutils literal"><span class="pre">Bio.SeqIO.index_db()</span></tt>.</p>
<p>Reasons to choose <tt class="docutils literal"><span class="pre">Bio.SeqIO.to_dict()</span></tt> over either
<tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> or <tt class="docutils literal"><span class="pre">Bio.SeqIO.index_db()</span></tt> boil down to a need
for flexibility despite its high memory needs. The advantage of storing
the <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects in memory is they can be changed, added to, or
removed at will. In addition to the downside of high memory consumption,
indexing can also take longer because all the records must be fully
parsed.</p>
<p>Both <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> and <tt class="docutils literal"><span class="pre">Bio.SeqIO.index_db()</span></tt> only parse
records on demand. When indexing, they scan the file once looking for
the start of each record and do as little work as possible to extract
the identifier.</p>
<p>Reasons to choose <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> over <tt class="docutils literal"><span class="pre">Bio.SeqIO.index_db()</span></tt>
include:</p>
<ul class="simple">
<li>Faster to build the index (more noticeable in simple file formats)</li>
<li>Slightly faster access as SeqRecord objects (but the difference is
only really noticeable for simple to parse file formats).</li>
<li>Can use any immutable Python object as the dictionary keys (e.g. a
tuple of strings, or a frozen set) not just strings.</li>
<li>Don’t need to worry about the index database being out of date if the
sequence file being indexed has changed.</li>
</ul>
<p>Reasons to choose <tt class="docutils literal"><span class="pre">Bio.SeqIO.index_db()</span></tt> over <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt>
include:</p>
<ul class="simple">
<li>Not memory limited – this is already important with files from second
generation sequencing where 10s of millions of sequences are common,
and using <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt> can require more than 4GB of RAM and
therefore a 64bit version of Python.</li>
<li>Because the index is kept on disk, it can be reused. Although
building the index database file takes longer, if you have a script
which will be rerun on the same datafiles in future, this could save
time in the long run.</li>
<li>Indexing multiple files together</li>
<li>The <tt class="docutils literal"><span class="pre">get_raw()</span></tt> method can be much faster, since for most file
formats the length of each record is stored as well as its offset.</li>
</ul>
</div>
</div>
<div class="section" id="writing-sequence-files">
<h2>5.5  Writing Sequence Files<a class="headerlink" href="#writing-sequence-files" title="Permalink to this headline">¶</a></h2>
<p>We’ve talked about using <tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> for sequence input
(reading files), and now we’ll look at <tt class="docutils literal"><span class="pre">Bio.SeqIO.write()</span></tt> which is
for sequence output (writing files). This is a function taking three
arguments: some <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects, a handle or filename to write to,
and a sequence format.</p>
<p>Here is an example, where we start by creating a few <tt class="docutils literal"><span class="pre">SeqRecord</span></tt>
objects the hard way (by hand, rather than by loading them from a file):</p>
<p>Now we have a list of <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects, we’ll write them to a FASTA
format file:</p>
<p>And if you open this file in your favourite text editor it should look
like this:</p>
<p>Suppose you wanted to know how many records the <tt class="docutils literal"><span class="pre">Bio.SeqIO.write()</span></tt>
function wrote to the handle? If your records were in a list you could
just use <tt class="docutils literal"><span class="pre">len(my_records)</span></tt>, however you can’t do that when your
records come from a generator/iterator. The <tt class="docutils literal"><span class="pre">Bio.SeqIO.write()</span></tt>
function returns the number of <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects written to the
file.</p>
<p><em>Note</em> - If you tell the <tt class="docutils literal"><span class="pre">Bio.SeqIO.write()</span></tt> function to write to a
file that already exists, the old file will be overwritten without any
warning.</p>
<div class="section" id="round-trips">
<h3>5.5.1  Round trips<a class="headerlink" href="#round-trips" title="Permalink to this headline">¶</a></h3>
<p>Some people like their parsers to be “round-tripable”, meaning if you
read in a file and write it back out again it is unchanged. This
requires that the parser must extract enough information to reproduce
the original file <em>exactly</em>. <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> does <em>not</em> aim to do this.</p>
<p>As a trivial example, any line wrapping of the sequence data in FASTA
files is allowed. An identical <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> would be given from parsing
the following two examples which differ only in their line breaks:</p>
<p>To make a round-tripable FASTA parser you would need to keep track of
where the sequence line breaks occurred, and this extra information is
usually pointless. Instead Biopython uses a default line wrapping of 60
characters on output. The same problem with white space applies in many
other file formats too. Another issue in some cases is that Biopython
does not (yet) preserve every last bit of annotation (e.g. GenBank and
EMBL).</p>
<p>Occasionally preserving the original layout (with any quirks it may
have) is important. See Section <a class="reference external" href="#sec:seqio-index-getraw">5.4.2.2</a>
about the <tt class="docutils literal"><span class="pre">get_raw()</span></tt> method of the <tt class="docutils literal"><span class="pre">Bio.SeqIO.index()</span></tt>
dictionary-like object for one potential solution.</p>
</div>
<div class="section" id="converting-between-sequence-file-formats">
<h3>5.5.2  Converting between sequence file formats<a class="headerlink" href="#converting-between-sequence-file-formats" title="Permalink to this headline">¶</a></h3>
<p>In previous example we used a list of <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects as input to
the <tt class="docutils literal"><span class="pre">Bio.SeqIO.write()</span></tt> function, but it will also accept a
<tt class="docutils literal"><span class="pre">SeqRecord</span></tt> iterator like we get from <tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> – this
lets us do file conversion by combining these two functions.</p>
<p>For this example we’ll read in the GenBank format file
<a class="reference external" href="http://biopython.org/DIST/docs/tutorial/examples/ls_orchid.gbk">ls_orchid.gbk</a>
and write it out in FASTA format:</p>
<p>Still, that is a little bit complicated. So, because file conversion is
such a common task, there is a helper function letting you replace that
with just:</p>
<p>The <tt class="docutils literal"><span class="pre">Bio.SeqIO.convert()</span></tt> function will take handles <em>or</em> filenames.
Watch out though – if the output file already exists, it will overwrite
it! To find out more, see the built in help:</p>
<p>In principle, just by changing the filenames and the format names, this
code could be used to convert between any file formats available in
Biopython. However, writing some formats requires information (e.g.
quality scores) which other files formats don’t contain. For example,
while you can turn a FASTQ file into a FASTA file, you can’t do the
reverse. See also Sections <a class="reference external" href="#sec:SeqIO-fastq-conversion">18.1.9</a>
and <a class="reference external" href="#sec:SeqIO-fasta-qual-conversion">18.1.10</a> in the cookbook
chapter which looks at inter-converting between different FASTQ formats.</p>
<p>Finally, as an added incentive for using the <tt class="docutils literal"><span class="pre">Bio.SeqIO.convert()</span></tt>
function (on top of the fact your code will be shorter), doing it this
way may also be faster! The reason for this is the convert function can
take advantage of several file format specific optimisations and tricks.</p>
</div>
<div class="section" id="converting-a-file-of-sequences-to-their-reverse-complements">
<h3>5.5.3  Converting a file of sequences to their reverse complements<a class="headerlink" href="#converting-a-file-of-sequences-to-their-reverse-complements" title="Permalink to this headline">¶</a></h3>
<p>Suppose you had a file of nucleotide sequences, and you wanted to turn
it into a file containing their reverse complement sequences. This time
a little bit of work is required to transform the <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects
we get from our input file into something suitable for saving to our
output file.</p>
<p>To start with, we’ll use <tt class="docutils literal"><span class="pre">Bio.SeqIO.parse()</span></tt> to load some nucleotide
sequences from a file, then print out their reverse complements using
the <tt class="docutils literal"><span class="pre">Seq</span></tt> object’s built in <tt class="docutils literal"><span class="pre">.reverse_complement()</span></tt> method (see
Section <a class="reference external" href="#sec:seq-reverse-complement">3.7</a>):</p>
<p>Now, if we want to save these reverse complements to a file, we’ll need
to make <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects. We can use the <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object’s
built in <tt class="docutils literal"><span class="pre">.reverse_complement()</span></tt> method (see
Section <a class="reference external" href="#sec:SeqRecord-reverse-complement">4.8</a>) but we must
decide how to name our new records.</p>
<p>This is an excellent place to demonstrate the power of list
comprehensions which make a list in memory:</p>
<p>Now list comprehensions have a nice trick up their sleeves, you can add
a conditional statement:</p>
<p>That would create an in memory list of reverse complement records where
the sequence length was under 700 base pairs. However, we can do exactly
the same with a generator expression - but with the advantage that this
does not create a list of all the records in memory at once:</p>
<p>As a complete example:</p>
<p>There is a related example in
Section <a class="reference external" href="#sec:SeqIO-translate">18.1.3</a>, translating each record in
a FASTA file from nucleotides to amino acids.</p>
</div>
<div class="section" id="getting-your-seqrecord-objects-as-formatted-strings">
<h3>5.5.4  Getting your SeqRecord objects as formatted strings<a class="headerlink" href="#getting-your-seqrecord-objects-as-formatted-strings" title="Permalink to this headline">¶</a></h3>
<p>Suppose that you don’t really want to write your records to a file or
handle – instead you want a string containing the records in a
particular file format. The <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> interface is based on handles,
but Python has a useful built in module which provides a string based
handle.</p>
<p>For an example of how you might use this, let’s load in a bunch of
<tt class="docutils literal"><span class="pre">SeqRecord</span></tt> objects from our orchids GenBank file, and create a string
containing the records in FASTA format:</p>
<p>This isn’t entirely straightforward the first time you see it! On the
bright side, for the special case where you would like a string
containing a <em>single</em> record in a particular file format, use the the
<tt class="docutils literal"><span class="pre">SeqRecord</span></tt> class’ <tt class="docutils literal"><span class="pre">format()</span></tt> method (see
Section <a class="reference external" href="#sec:SeqRecord-format">4.5</a>).</p>
<p>Note that although we don’t encourage it, you <em>can</em> use the <tt class="docutils literal"><span class="pre">format()</span></tt>
method to write to a file, for example something like this:</p>
<p>While this style of code will work for a simple sequential file format
like FASTA or the simple tab separated format used here, it will <em>not</em>
work for more complex or interlaced file formats. This is why we still
recommend using <tt class="docutils literal"><span class="pre">Bio.SeqIO.write()</span></tt>, as in the following example:</p>
<p>Making a single call to <tt class="docutils literal"><span class="pre">SeqIO.write(...)</span></tt> is also much quicker than
multiple calls to the <tt class="docutils literal"><span class="pre">SeqRecord.format(...)</span></tt> method.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Chapter 5  Sequence Input/Output</a><ul>
<li><a class="reference internal" href="#parsing-or-reading-sequences">5.1  Parsing or Reading Sequences</a><ul>
<li><a class="reference internal" href="#reading-sequence-files">5.1.1  Reading Sequence Files</a></li>
<li><a class="reference internal" href="#iterating-over-the-records-in-a-sequence-file">5.1.2  Iterating over the records in a sequence file</a></li>
<li><a class="reference internal" href="#getting-a-list-of-the-records-in-a-sequence-file">5.1.3  Getting a list of the records in a sequence file</a></li>
<li><a class="reference internal" href="#extracting-data">5.1.4  Extracting data</a></li>
</ul>
</li>
<li><a class="reference internal" href="#parsing-sequences-from-compressed-files">5.2  Parsing sequences from compressed files</a></li>
<li><a class="reference internal" href="#parsing-sequences-from-the-net">5.3  Parsing sequences from the net</a><ul>
<li><a class="reference internal" href="#parsing-genbank-records-from-the-net">5.3.1  Parsing GenBank records from the net</a></li>
<li><a class="reference internal" href="#parsing-swissprot-sequences-from-the-net">5.3.2  Parsing SwissProt sequences from the net</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sequence-files-as-dictionaries">5.4  Sequence files as Dictionaries</a><ul>
<li><a class="reference internal" href="#sequence-files-as-dictionaries-in-memory">5.4.1  Sequence files as Dictionaries – In memory</a><ul>
<li><a class="reference internal" href="#specifying-the-dictionary-keys">5.4.1.1  Specifying the dictionary keys</a></li>
<li><a class="reference internal" href="#indexing-a-dictionary-using-the-seguid-checksum">5.4.1.2  Indexing a dictionary using the SEGUID checksum</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sequence-files-as-dictionaries-indexed-files">5.4.2  Sequence files as Dictionaries – Indexed files</a><ul>
<li><a class="reference internal" href="#id1">5.4.2.1  Specifying the dictionary keys</a></li>
<li><a class="reference internal" href="#getting-the-raw-data-for-a-record">5.4.2.2  Getting the raw data for a record</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sequence-files-as-dictionaries-database-indexed-files">5.4.3  Sequence files as Dictionaries – Database indexed files</a><ul>
<li><a class="reference internal" href="#id2">5.4.3.1  Getting the raw data for a record</a></li>
</ul>
</li>
<li><a class="reference internal" href="#indexing-compressed-files">5.4.4  Indexing compressed files</a></li>
<li><a class="reference internal" href="#discussion">5.4.5  Discussion</a></li>
</ul>
</li>
<li><a class="reference internal" href="#writing-sequence-files">5.5  Writing Sequence Files</a><ul>
<li><a class="reference internal" href="#round-trips">5.5.1  Round trips</a></li>
<li><a class="reference internal" href="#converting-between-sequence-file-formats">5.5.2  Converting between sequence file formats</a></li>
<li><a class="reference internal" href="#converting-a-file-of-sequences-to-their-reverse-complements">5.5.3  Converting a file of sequences to their reverse complements</a></li>
<li><a class="reference internal" href="#getting-your-seqrecord-objects-as-formatted-strings">5.5.4  Getting your SeqRecord objects as formatted strings</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="chr4.html"
                        title="previous chapter">Chapter 4  Sequence annotation objects</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="chr6.html"
                        title="next chapter">Chapter 6  Multiple Sequence Alignment objects</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/chr5.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="chr6.html" title="Chapter 6 Multiple Sequence Alignment objects"
             >next</a> |</li>
        <li class="right" >
          <a href="chr4.html" title="Chapter 4 Sequence annotation objects"
             >previous</a> |</li>
        <li><a href="index.html">Biopython_en 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2013, Biopython.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
    </div>
  </body>
</html>